﻿using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using Transport.Areas.Administrator.Models;
using Transport.Core.Models.Settings;
using Transport.Infrastructure.Mvc;
using Transport.Infrastructure.Security;
using Transport.Services;
using Transport.Services.Infrastructure;
using Transport.Services.Settings;

namespace Transport.Areas.Administrator.Controllers
{
    public class PostOfficeController : ControllerBase<IPostOfficeRepository, PostOffice>
    {
        private readonly IStateProvinceRepository _stateProvinceRepository;
        public PostOfficeController(IUnitOfWork unitOfWork, IPostOfficeRepository repository,
                                    IStateProvinceRepository stateProvinceRepository)
            : base(repository, unitOfWork)
        {
            _stateProvinceRepository = stateProvinceRepository;
        }

        //
        // GET: /Administrator/PostOffice/

        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult GridModel(string search)
        {
            var model = Repository.Search(search).Include(p => p.StateProvince);

            var gridModel = new GridModel<PostOfficeModel>
            {
                Data = model.Select(x => new PostOfficeModel
                                             {
                                                 Address = x.Address,
                                                 PhoneNumber = x.PhoneNumber,
                                                 PostOfficeCode = x.PostOfficeCode,
                                                 PostOfficeId = x.PostOfficeId,
                                                 PostOfficeName = x.PostOfficeName,
                                                 StateProvinceName = x.StateProvince.StateProvinceName
                                             })
            };
            return new JsonResult
            {
                Data = gridModel
            };
        }

        public ActionResult Create()
        {
            var stateProvinces = _stateProvinceRepository.Search("").ToList();
            var model = new PostOfficeModel()
            {
                StateProvinces = stateProvinces
            };

            return View(model);
        }

        public virtual ActionResult Edit(int id)
        {
            var stateProvinces = _stateProvinceRepository.Search("").ToList();
            var entity = Repository.GetById(id);
            var model = new PostOfficeModel()
            {
                StateProvinces = stateProvinces,
                Address = entity.Address,
                PhoneNumber = entity.PhoneNumber,
                PostOfficeCode = entity.PostOfficeCode,
                PostOfficeId = entity.PostOfficeId,
                PostOfficeName = entity.PostOfficeName,
                StateProvinceId = entity.StateProvinceId,
            };
            return View("Edit", model);
        }

        [HttpPost, ValidateInput(false)]
        public virtual ActionResult Save(PostOfficeModel postOfficeModel)
        {
            if (!ModelState.IsValid)
            {
                var stateProvinces = _stateProvinceRepository.Search("").ToList();
                postOfficeModel.StateProvinces = stateProvinces;
                return View("Create", postOfficeModel);
            }
            //Check existed
            var modelExisted = Repository.GetAll().FirstOrDefault(p => p.PostOfficeCode == postOfficeModel.PostOfficeCode || p.PostOfficeName == postOfficeModel.PostOfficeName);
            if ((modelExisted != null && modelExisted.PostOfficeId != postOfficeModel.PostOfficeId) || (modelExisted != null && postOfficeModel.PostOfficeId <= 0))
            {
                this.SetErrorNotification("Bưu cục này đã tồn tại trong hệ thống.");
                var stateProvinces = _stateProvinceRepository.Search("").ToList();
                postOfficeModel.StateProvinces = stateProvinces;
                return View("Create", postOfficeModel);
            }
            if (postOfficeModel.PostOfficeId <= 0) //Create
            {
                var entity = new PostOffice()
                {
                    Address = postOfficeModel.Address,
                    IsDeleted = false,
                    PhoneNumber = postOfficeModel.PhoneNumber,
                    PostOfficeCode = postOfficeModel.PostOfficeCode,
                    PostOfficeName = postOfficeModel.PostOfficeName,
                    StateProvinceId = postOfficeModel.StateProvinceId,
                };
                using (UnitOfWork)
                {
                    Repository.Insert(entity);
                }
            }
            else //Edit
            {
                var entity = Repository.GetById(postOfficeModel.PostOfficeId);
                entity.Address = postOfficeModel.Address;
                entity.PhoneNumber = postOfficeModel.PhoneNumber;
                entity.PostOfficeCode = postOfficeModel.PostOfficeCode;
                entity.PostOfficeName = postOfficeModel.PostOfficeName;
                entity.StateProvinceId = postOfficeModel.StateProvinceId;
                using (UnitOfWork)
                {
                    Repository.Update(entity);
                }
            }

            //Save success
            this.SetSuccessNotification(string.Format("{0} đã được lưu thành công.", "Bưu cục"));
            return RedirectToAction("Index", new { area = "Administrator" });
        }

        public ActionResult Delete(int id)
        {
            try
            {
                using (UnitOfWork)
                {
                    Repository.Delete(id);
                }
                this.SetSuccessNotification("Bưu cục đã được xóa thành công.");
            }
            catch
            {
                this.SetErrorNotification("Bưu cục này không thể xóa, vì đã được sử dụng!");
            }
            return RedirectToAction("index", new { area = "Administrator" });
        }
    }
}
